<?php

/**
 * Description of owners
 *
 * @author Shermor Gaillard 
 */

 
 class Owners_Info extends Model {
                /*
                 * Querys and returns all owners in database
                 */
		public function retrieve_owners()
		{
		 
		 return $this->db->query('SELECT CONCAT_WS(\', \', o.lname , o.fname ) as fullname, o.owner_id, o.lname, p.name  FROM owners o INNER JOIN pets p ON o.owner_id = p.owner_id;')->result();
		 
		}
                //----------------------------
                /*
                 * Queries database and returns owners sorted by last name aphabetically
                 */
		public function orderby_owners()
		{

		 return $this->db->query('SELECT CONCAT_WS(\', \', o.lname , o.fname ) as name, o.owner_id, o.lname, p.pet_name  FROM owners o INNER JOIN pets p ON o.owner_id = p.owner_id GROUP BY o.lname ORDER BY o.lname;')->result();
		 
		}
                //----------------------------------
                /*
                 * Retrieve details for an owner including the pets they own
                 * @param $where used to select the correct owner id
                 */
		public function retrieve_details($where)
		{
				$sql = "SELECT o.*,o.owner_notes as owner_notes, p.* FROM owners o INNER JOIN pets p ON o.owner_id = p.owner_id where o.owner_id = ?;";
				return $this->db->query($sql,array($where))->result();
		}
                //-------------------------------
                /*
                 * Retrieve owner id for a specific owner
                 * @param $fname (firstname of owner)
                 * @param $lname (lastname of owner
                 */
		public function retrieve_id($fname, $lname)
		{

				$sql = "SELECT o.* FROM owners o where o.fname = ? and o.lname = ?;";
				return $this->db->query($sql,array($fname,$lname))->result();
		}
                //---------------------
                /*
                 * Insert a new owner into the database
                 * @param $fname
                 * @param $lname
                 * @param $address
                 * @param $city
                 * @param $state
                 * @param $zip
                 * @param $phoneH
                 * @param $phoneC
                 * @param $phoneW
                 * @param $email
                 * @param $notes
                 */
		public function insert_owners($fname,$lname,$address,$city,$state,$zip,$phoneH, $phoneC, $phoneW,$email, $notes){
				$data = array('fname'=>$fname, 'lname'=>$lname, 'street'=>$address, 'city'=>$city, 'state'=>$state, 'zip'=>$zip, 'home_phone'=>$phoneC, 'cell_phone'=>$phoneW,'work_phone'=>$phoneH,'email'=>$email,'owner_notes'=>$notes);

				$str = $this->db->insert_string('owners', $data);
				
				$query = $this->db->query($str);


		}
                 //---------------------
                /*
                 * Update an owner record in the database
                 * @param $id
                 * @param $fname
                 * @param $lname
                 * @param $address
                 * @param $city
                 * @param $state
                 * @param $zip
                 * @param $phoneH
                 * @param $phoneC
                 * @param $phoneW
                 * @param $email
                 * @param $notes
                 */
		public function update_owners($id,$fname,$lname,$address,$city,$state,$zip,$phoneH, $phoneC, $phoneW,$email, $notes ){


				$data = array('fname'=>$fname, 'lname'=>$lname, 'street'=>$address, 'city'=>$city, 'state'=>$state, 'zip'=>$zip, 'home_phone'=>$phoneC, 'cell_phone'=>$phoneW,'work_phone'=>$phoneH,'email'=>$email,'owner_notes'=>$notes);

				$where = "owner_id = $id";

				$str = $this->db->update_string('owners', $data, $where);
				
				$query = $this->db->query($str);
		}
                /*
                 * Delete a record in the database
                 * @param $where
                 */
		 public function delete_owners($where){
		 $sql = "DELETE FROM owners where id = ?;";
     return $this->db->query($sql,array($where));
    }

//*EOF*//	
}	
